package com.yiren.manager.ctrl.test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderTestResultRecord;
import com.yiren.entity.YrTestResultItem;
import com.yiren.entity.YrTestTopic;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.elder.YrElderForm;
import com.yiren.manager.form.test.YrElderTestResultRecordForm;
import com.yiren.manager.form.test.YrTestResultItemForm;
import com.yiren.manager.form.test.YrTestTopicForm;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.test.YrElderTestResultRecordService;
import com.yiren.manager.service.iface.test.YrTestResultItemService;
import com.yiren.manager.service.iface.test.YrTestTopicService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("elderTestResultRecord")
public class YrElderTestResultRecordController extends GenericController<YrElderTestResultRecordForm, YrElderTestResultRecord>{

	@Autowired
	private YrElderTestResultRecordService selfService;
	
	@Autowired
	private YrTestResultItemService testResultItemService;
	
	@Autowired
	private YrTestTopicService testTopicService;
	
	@Autowired
	private YrElderService elderService;
	
	@RequestMapping("findElderTestResult")
	@ResponseBody
	public HTTPResult<YrElderTestResultRecordForm> findElderTestResult(String elderId,String testTopicId,Integer testTimesNumber) throws Exception{
		HTTPResult<YrElderTestResultRecordForm> result = new HTTPResult<YrElderTestResultRecordForm>();
		if(null==elderId||null==testTopicId||testTimesNumber==null){
			throwExp("参数有误");
		}
		YrElderTestResultRecordForm form = new YrElderTestResultRecordForm();	
		
		YrElder elder = elderService.findOne(elderId);
		if(null==elder){
			throwExp("查不到相关老者信息");
		}

		YrTestTopic testTopic = testTopicService.findOne(testTopicId);
		if(null==testTopic){
			throwExp("查不到题目信息");
		}
	
		String sqlWhere = "elder_id=?1 AND test_topic_id=?2 AND test_times_number=?3";
		QueryResult<YrElderTestResultRecord> qr = selfService.find(YrElderTestResultRecord.class, sqlWhere, new Object[]{elderId,testTopicId,testTimesNumber});
		if(qr.getTotalRecord()==0){
			throwExp("该老者尚未测试此题");
		}
		form = toForm(qr.getResultList().get(0));
		form.setElderForm(ObjectUtils.objectToObject(elder, YrElderForm.class));
		form.setTestTopicForm(ObjectUtils.objectToObject(testTopic, YrTestTopicForm.class));
		sqlWhere = "test_topice_id=?1";
		QueryResult<YrTestResultItem> qrTestResultItem =testResultItemService.find(YrTestResultItem.class, sqlWhere, new Object[]{testTopicId});
		
		List<YrElderTestResultRecordForm> formList = new ArrayList<YrElderTestResultRecordForm>();
		
		List<YrTestResultItemForm> testResultItemForms = new ArrayList<YrTestResultItemForm>();
		for(YrTestResultItem testResultItem : qrTestResultItem.getResultList()){
			testResultItemForms.add(ObjectUtils.objectToObject(testResultItem, YrTestResultItemForm.class));
		}
		form.setTestResultItemForms(testResultItemForms);
		formList.add(form);
		
		System.out.println(form.getElderForm().getName());
		System.out.println(form.getTestTopicForm().getContent());
		
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrElderTestResultRecordForm toForm(YrElderTestResultRecord e) throws Exception {
		YrElderTestResultRecordForm form = ObjectUtils.objectToObject(e, YrElderTestResultRecordForm.class);
		return form;
	}

	@Override
	public YrElderTestResultRecord toEntity(YrElderTestResultRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
